package com.xflib.shardingsphere.controller;

import com.xflib.shardingsphere.entity.User;
import com.xflib.shardingsphere.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户 API
 *
 * @author Jiahai
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 新增
     *
     * @return
     */
    @PostMapping
    public User save(@RequestBody User user) {
        return userService.save(user);
    }

    /**
     * 删除
     *
     * @param userId 用户ID
     */
    @DeleteMapping("/{userId}")
    public void delete(@PathVariable Long userId) {
        userService.delete(userId);
    }

    /**
     * 修改
     *
     * @param user 用户
     */
    @PutMapping
    public User update(@RequestBody User user){
        return userService.update(user);
    }

    /**
     * 根据主键ID查询
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/{userId}")
    public User findById(@PathVariable Long userId) {
        return userService.findById(userId);
    }

    /**
     * 列表查询（根据主键ID顺序）
     *
     * @return
     */
    @GetMapping("/list")
    public List<User> list() {
        return userService.list();
    }

    /**
     * 分页查询（根据主键ID逆序）
     *
     * @param page 当前页
     * @param size 每页条数
     * @return Page<User> 分页对象
     */
    @GetMapping("/page/{page}/{size}")
    public Page<User> page(@PathVariable int page, @PathVariable int size) {
        return userService.page(page, size);
    }
}
